package com.ssm.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ssm.auth.application.convert.AuthPermissionDtoConvert;
import com.ssm.auth.application.dto.AuthPermissionDTO;
import com.ssm.auth.entity.Result;
import com.ssm.auth.domain.bo.AuthPermissionBO;
import com.ssm.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission")
@Log4j2
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     *  新增权限
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父级id不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getPermissionKey(), "权限key不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(), "权限类型不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDtoConvert.INSTANCE.AuthPermissionDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.info("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     *  更新权限
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDtoConvert.INSTANCE.AuthPermissionDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.info("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新权限失败");
        }
    }

    /**
     *  删除权限
     * @param authPermissionDTO
     * @return
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDtoConvert.INSTANCE.AuthPermissionDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.info("PermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除权限失败");
        }
    }

    /**
     * 查询用户权限
     */
    @RequestMapping("/getPermission")
    public Result<Boolean> getPermission(String userName) {
        try {
            log.info("PermissionController.getPermission.userName:{}", userName);
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户id不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询用户权限信息失败");
        }
    }

}
